/*
 *   m_ircops - /IRCOPS command that lists IRC Operators
 *   (C) Copyright 2004-2005 Syzop <syzop@vulnscan.org>
 *   (C) Copyright 2003-2004 AngryWolf <angrywolf@flashmail.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 1, or (at your option)
 *   any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "config.h"
#include "struct.h"
#include "common.h"
#include "sys.h"
#include "numeric.h"
#include "msg.h"
#include "proto.h"
#include "channel.h"
#include <time.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <io.h>
#endif
#include <fcntl.h>
#include "h.h"
#ifdef STRIPBADWORDS
#include "badwords.h"
#endif
#ifdef _WIN32
#include "version.h"
#endif

typedef struct
{
	long *umode;
	char *text;
} oflag;

#define MSG_STAFF	  "STAFF"
#define TOK_STAFF	  NULL
#define MSG_IRCOPS        "IRCOPS"
#define TOK_IRCOPS        NULL
#define MSG_HELPOPS	  "HELPOPS"
#define TOK_HELPOPS	  NULL

#define IsAway(x)         (x)->user->away

#if !defined(IsSkoAdmin)
#define IsSkoAdmin(sptr)  (IsAdmin(sptr) || IsNetAdmin(sptr) || IsSAdmin(sptr) || IsCoAdmin(sptr))
#endif

void SendHelpopList(aClient *pClient);
void SendIrcopList(aClient *pClient);
static int m_staff (aClient *cptr, aClient *sptr, int parc, char *parv[]);
static int m_ircops(aClient *cptr, aClient *sptr, int parc, char *parv[]);
static int m_helpops(aClient *cptr, aClient *sptr, int parc, char *parv[]);

static oflag otypes[7];

char *pszHelpopAvailable = "available";
char *pszHelpopNotAvailable = "not available";

ModuleHeader MOD_HEADER(m_staff)
  = {
	"m_staff",
	"v1.0",
	"Adds /staff, /ircops, and /helpops commands for listing staff members.  Based on m_ircops by AngryWolf/Syzop",
	"3.2-b8-1",
	NULL 
    };

DLLFUNC int MOD_INIT(m_staff)(ModuleInfo *modinfo)
{
	otypes[0].umode = &UMODE_NETADMIN;
	otypes[0].text = "a Network Administrator";
	otypes[1].umode = &UMODE_SADMIN;
	otypes[1].text = "a Services Administrator";
	otypes[2].umode = &UMODE_ADMIN;
	otypes[2].text = "a Server Administrator";
	otypes[3].umode = &UMODE_COADMIN;
	otypes[3].text = "a Co Administrator";
	otypes[4].umode = &UMODE_OPER;
	otypes[4].text = "an IRC Operator";
	otypes[5].umode = &UMODE_LOCOP;
	otypes[5].text = "a Local Operator";
	otypes[6].umode = NULL;
	otypes[6].text = NULL;

	if (CommandExists(MSG_STAFF))
	{
		config_error("Command " MSG_STAFF " already exists");
		return MOD_FAILED;
	}

	if (CommandExists(MSG_IRCOPS))
	{
		config_error("Command " MSG_IRCOPS " already exists");
		return MOD_FAILED;
	}

	if (CommandExists(MSG_HELPOPS))
	{
		config_error("Command " MSG_HELPOPS " already exists");
		return MOD_FAILED;
	}

	CommandAdd(modinfo->handle, MSG_STAFF, TOK_STAFF, m_staff, MAXPARA, M_USER);

	if (ModuleGetError(modinfo->handle) != MODERR_NOERROR)
        {
                config_error("Error adding command " MSG_STAFF ": %s",
                        ModuleGetErrorStr(modinfo->handle));
                return MOD_FAILED;
        }

	CommandAdd(modinfo->handle, MSG_IRCOPS, TOK_IRCOPS, m_ircops, MAXPARA, M_USER);
	
	if (ModuleGetError(modinfo->handle) != MODERR_NOERROR)
        {
                config_error("Error adding command " MSG_IRCOPS ": %s",
                        ModuleGetErrorStr(modinfo->handle));
                return MOD_FAILED;
        }

	CommandAdd(modinfo->handle, MSG_HELPOPS, TOK_HELPOPS, m_helpops, MAXPARA, M_USER);

	if (ModuleGetError(modinfo->handle) != MODERR_NOERROR)
	{
		config_error("Error adding command " MSG_HELPOPS ": %s",
			ModuleGetErrorStr(modinfo->handle));
		return MOD_FAILED;
	}

	return MOD_SUCCESS;
}

DLLFUNC int MOD_LOAD(m_staff)(int module_load)
{
	return MOD_SUCCESS;
}

DLLFUNC int MOD_UNLOAD(m_staff)(int module_unload)
{
	return MOD_SUCCESS;
}


static char *find_otype(long umodes)
{
	unsigned int i;
	
	for (i = 0; otypes[i].umode; i++)
		if (*otypes[i].umode & umodes)
			return otypes[i].text;

	return "an unknown operator";
}

/*
 * m_ircops
 *
 *     parv[0]: sender prefix
 *
 *     Originally comes from TR-IRCD, but I changed it in several places.
 *     In addition, I didn't like to display network name. In addition,
 *     instead of realname, servername is shown. See the original
 *     header below.
 */

/************************************************************************
 * IRC - Internet Relay Chat, modules/m_ircops.c
 *
 *   Copyright (C) 2000-2002 TR-IRCD Development
 *
 *   Copyright (C) 1990 Jarkko Oikarinen and
 *                      University of Oulu, Co Center
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


static int m_staff(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	sendto_one(sptr, rpl_str(RPL_STARTOFHELPERS), me.name, 
		sptr->name);

	SendHelpopList(sptr);
	
	sendto_one(sptr, rpl_str(RPL_STARTOFIRCOPS), me.name,
		sptr->name);

	SendIrcopList(sptr);

	sendto_one(sptr, rpl_str(RPL_ENDOFSTAFF), me.name,
		sptr->name);

	return 0;
}

static int m_helpops(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	sendto_one(sptr, rpl_str(RPL_STARTOFHELPERS), me.name,
		sptr->name);

	SendHelpopList(sptr);

	sendto_one(sptr, rpl_str(RPL_ENDOFHELPERS), me.name, sptr->name);

	return 0;
}


static int m_ircops(aClient *cptr, aClient *sptr, int parc, char *parv[])
{
	sendto_one(sptr, rpl_str(RPL_STARTOFIRCOPS), me.name,
		sptr->name);

	SendIrcopList(sptr);
	
	sendto_one(sptr, rpl_str(RPL_ENDOFIRCOPS), me.name, sptr->name);

	return 0;
}

void SendHelpopList(aClient *pClient)
{
	aClient *pTemp;

	int iAway = 0;
	int iTotal = 0;
		
	for (pTemp = client; pTemp != NULL; pTemp = pTemp->next)
	{
		if (!IsAnOper(pTemp) && (pTemp->umodes & UMODE_HELPOP))
		{
			sendto_one(pClient, rpl_str(RPL_HELPER), me.name,
				pClient->name, pTemp->name, 
				(IsAway(pTemp) ? pszHelpopNotAvailable : pszHelpopAvailable));

			if (IsAway(pTemp))
			{
				iAway++;
			}

			iTotal++;
		}
	}

	sendto_one(pClient, rpl_str(RPL_HELPERSTATS), me.name, 
		pClient->name, (iTotal == 1) ? "is" : "are", 
		iTotal, (iTotal == 1) ? "" : "s");
}


void SendIrcopList(aClient *pClient)
{
	aClient *pTemp;

	int iTotal = 0;
	int iAdmins = 0;
	int iAway = 0;

	for (pTemp = client; pTemp != NULL; pTemp = pTemp->next)
	{
		if (IsAnOper(pTemp) && !IsULine(pTemp) 
			&& IsPerson(pTemp) 
			&& (!IsHideOper(pTemp) || IsAnOper(pClient)))
		{
			if (IsAway(pTemp))
			{
				sendto_one(pClient, rpl_str(RPL_IRCOP),
					me.name, pClient->name, pTemp->name,
					find_otype(pTemp->umodes),
					"[Away]");

				iAway++;
			}
			else
			{
				 sendto_one(pClient, rpl_str(RPL_IRCOP),
                                        me.name, pClient->name, pTemp->name,
                                        find_otype(pTemp->umodes),
                                        "[Active]");
			}
			
			if (IsSkoAdmin(pTemp))
			{
				iAdmins++;
			}

			iTotal++;
		}
	}

	sendto_one(pClient, rpl_str(RPL_IRCOPSTATS), me.name, 
		pClient->name, (iTotal == 1) ? "is" : "are", iTotal,
		(iTotal == 1) ? "" : "s");
}

